<template>
  <MyModal ref="myModal" title="分组管理" :top="25" :width="800">
    <template #content>
      <div style="margin-bottom: 5px">
        <a-button type="primary" style="border-radius: 6px" @click="add">添加分组</a-button>
      </div>

      <a-table style="overflow-x: scroll" :columns="columns" :data-source="dataSource" :pagination="pagination"
        @change="changePage">
        <template #emptyText>
          <a-empty description="暂无数据" />
        </template>

        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'id'">
            <span>{{ record.id }}</span>
          </template>

          <template v-if="column.key === 'groupName'">
            <!-- <span>
              <a-input
                @blur="editGroupName(record)"
                v-model:value="record.groupName"
                style="border: 0"
              />
            </span> -->
            <div style="display: flex; align-items: center">
              <div>备注：</div>
              <span v-if="!record.isRemarkShow">{{ record.groupName }}</span>
              <a-input v-else v-model:value="record.groupName" @blur="editGroupName(record)"
                style="width: 160px; border: 0" />
              <div>
                <EditOutlined @click="EditRemarkSet(record)" v-if="!record.isRemarkShow"
                  style="margin-left: 10px; color: #1890ff" />
              </div>
            </div>
          </template>

          <template v-else-if="column.key === 'action'">
            <div style="width: 100px; margin: 0; padding: 0">
              <a-popconfirm title="确定删除？" ok-text="是" cancel-text="否" @confirm="delAccount(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </div>
          </template>
        </template>
      </a-table>

      <AddGroup ref="addGroupRef" @refresh="getList"></AddGroup>
    </template>
  </MyModal>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import AddGroup from './AddGroup.vue'
import { message } from 'ant-design-vue'
import * as fishGoodsApi from '@/api/fishGoods'


const myModal = ref()
const addGroupRef = ref()

// let onlyType = ref<string>('wx')
const showModal = () => {
  myModal.value?.showModal()
  getList()
}

const emit = defineEmits<{
  refresh: [],
  onRefreshList:[]
}>()

defineExpose({
  showModal,
})

const EditRemarkSet = async (record: any) => {
  record.isRemarkShow = true
}
const dataSource = ref([])
const columns = [
  {
    title: 'ID',
    key: 'id',
    width: 130,
  },
  {
    title: '分组名称',
    key: 'groupName',
    width: 150,
  },
  {
    title: '操作',
    key: 'action',
    width: 90,
  },
]

interface Pagination {
  page: number
  pageSize: number
  total: number
  current?: number
}
const pagination = ref<Pagination>({ page: 1, pageSize: 10, total: 0 })

interface SearchParams {
  currentPage: number
  pageSize: number
}
const searchParams = ref<SearchParams>({
  currentPage: pagination.value.page,
  pageSize: pagination.value.pageSize,
})

// 翻页
const changePage = (e: any) => {
  pagination.value.current = e.current
  pagination.value.pageSize = e.pageSize

  searchParams.value.currentPage = pagination.value.current as number
  getList()
}

// 列表
const getList = async () => {
  let api = fishGoodsApi.getFishGoodsList()

  // onlyType.value === 'wx'
  //   ? (api = fishGoodsApi.getFishGoodsList())
  //   : (api = fishGoodsApi.getFishRobotGroupList())

  try {
    let { state, data, message: msg } = await api
    if (state === 200) {
      data.forEach((item: { isRemarkShow: boolean }) => {
        item.isRemarkShow = false
      })
      dataSource.value = data
      emit("onRefreshList")
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
}

// 编辑
const editGroupName = async (record: any) => {
  const params = {
    groupId: record.id,
    groupName: record.groupName,
  }
  let api = fishGoodsApi.editFishGoodsGroup(params)

  try {
    let { state, message: msg } = await api
    if (state === 200) {
      message.success('修改成功~')
      getList()
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
}

// 删除
const delAccount = async (id: string) => {
  let api = fishGoodsApi.delFishGoodsGroup({ id })

  try {
    let { state, message: msg } = await api
    if (state === 200) {
      message.success('删除成功~')
      getList()
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
}

// 添加分组
const add = () => {
  addGroupRef.value.showModal()
}
</script>
